// pages/couponActivity/couponDetail/index.js
const couponDetail=require("../../../controllers/ControllerX").controller.couponDetail;
const couponPush=require("../../../controllers/ControllerX").controller.couponPush;
const couponDelete=require("../../../controllers/ControllerX").controller.couponDelete;

const app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:null,
    isPush:'',
    couponData:{},
    state1:false,
    state2:false,
    state3:false,
    state4:false,
    state5:false,
    state6:false,
    state7:false,
    state8:false,
    state9:false,
    state10:false,
    isloading:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id=options.id;
    let isPush=options.ispush;
    this.setData({
      id:id,
      isPush:isPush
    })
    console.log(this.data.isPush);
    console.log(id);
    this.getCouponDetail()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // },

  //获取优惠券详情
  getCouponDetail(){
    let shopId=wx.getStorageSync('shopId');
    couponDetail({shopId:shopId,couponId:this.data.id}).then(res=>{
      if(res.code==1){
        this.setData({
          couponData:res.data
        })
        let nowTime=new Date().getTime();
        let end=res.data.endTime;
        let endTime=new Date(end.replace(/-/g,"/")).getTime();
        console.log(nowTime-endTime);
        if(nowTime>endTime){
          this.setData({
            isPush:2
          })
        }
      }
      this.judgeType();

    })
  },
  //判断是哪种优惠券并隐藏多余的项
  judgeType(){
    if(this.data.couponData.type==0){ //现金抵用券
      this.setData({
        state1:true,
        state2:false,
        state3:false,
        state4:false,
        state5:false,
        state6:false,
        state7:true,
        state8:false,
        state9:false,
        state10:false
      })
    }else if(this.data.couponData.type==1){  //满减券
      this.setData({
        state1:false,
        state2:true,
        state3:false,
        state4:false,
        state5:false,
        state6:false,
        state7:true,
        state8:false,
        state9:false,
        state10:false
      })
    }else if(this.data.couponData.type==2){   //限时
      this.setData({
        state1:false,
        state2:false,
        state3:false,
        state4:false,
        state5:true,
        state6:false,
        state7:false,
        state8:true,
        state9:false,
        state10:true
      })
    }else if(this.data.couponData.type==3){     //免费体验
      this.setData({
        state1:false,
        state2:false,
        state3:false,
        state4:true,
        state5:false,
        state6:false,
        state7:false,
        state8:true,
        state9:false,
        state10:true
      })
    }else if(this.data.couponData.type==4){     //活动礼包
      this.setData({
        state1:false,
        state2:false,
        state3:false,
        state4:false,
        state5:false,
        state6:true,
        state7:false,
        state8:false,
        state9:true,
        state10:true
      })
    }else{
      this.setData({      //拉新
        state1:false,
        state2:false,
        state3:true,
        state4:false,
        state5:false,
        state6:false,
        state7:true,
        state8:false,
        state9:false,
        state10:false
      })
    }
  },

  //优惠券推送
  getcouponPush(){
    console.log(this.data.isPush)
    if(this.data.isPush==0){
      let id=this.data.id;
      let shopId=wx.getStorageSync('shopId');
      if(this.data.isloading){
        return
      }
      this.setData({
        isloading:true
      })
      couponPush({shopId:shopId,couponId:id}).then(res=>{
        if(res.code==1){
          wx.showToast({
            icon:"none",
            title: '优惠券推送成功',
          })
          this.setData({
            'isPush':1
          })
        }
        this.setData({
          isloading:false
        })
      }).catch(res=>{
        this.setData({
          isloading:false
        })
      })
    }
  },

  //删除优惠券
  getcouponDelete(){
    let shopId=wx.getStorageSync('shopId');
    couponDelete({shopId:shopId,couponId:this.data.id}).then(res=>{
      if(res.code==1){
        wx.showToast({
          icon:"none",
          title: '删除成功',
          duration:1500
        })

        setTimeout(function(){
          wx.navigateBack()
        },1500)
      }
    })
  },

  //跳转优惠券统计详情
  toCouponStatic(){
    let id=this.data.couponData.id;
    let pushNum=this.data.couponData.pushNum;
    let receiveNum=this.data.couponData.receiveNum;
    let useNum=this.data.couponData.useNum;
    let name=this.data.couponData.name;
    let startTime=this.data.couponData.startTime.split(" ")[0];
    let endTime=this.data.couponData.endTime.split(" ")[0];
    let type=this.data.couponData.type;
    wx.navigateTo({
      url: '/pages/Index/couponActivity/couponStatistics/couponStatistics?id='+id+'&pushNum='+pushNum+'&receiveNum='+receiveNum+'&useNum='+useNum+'&name='+name+'&startTime='+startTime+'&endTime='+endTime+'&type='+type,
    })
  }
})